package com.fhz.huanxintest

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.fhz.huanxintest.databinding.ActivityMainBinding
import com.hyphenate.chat.AgoraMessage
import com.hyphenate.chat.ChatClient
import com.hyphenate.helpdesk.callback.Callback
import com.hyphenate.helpdesk.easeui.util.IntentBuilder
import kotlin.concurrent.thread


class MainActivity : AppCompatActivity() {
    lateinit var binding:ActivityMainBinding
    private  val TAG = "MainActivity"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)

        setContentView(binding.root)

        binding.login.setOnClickListener{

            ChatClient.getInstance().login("feng1", "feng1", object : Callback {
                override fun onSuccess() {
                    Log.d(TAG, "onSuccess: ")
                }

                override fun onError(code: Int, error: String?) {
                    Log.d(TAG, "onError: $error")
                }

                override fun onProgress(progress: Int, status: String?) {
                    Log.d(TAG, "onProgress: $status")
                }
            })

        }

        binding.register.setOnClickListener{
            thread {
                ChatClient.getInstance()
                    .register("feng1", "feng1",
                        object : Callback {
                            override fun onSuccess() {
                                Log.d(TAG, "onSuccess: ")
                            }

                            override fun onError(code: Int, error: String?) {
                                Log.d(TAG, "onError: $error")
                            }

                            override fun onProgress(progress: Int, status: String?) {
                                Log.d(TAG, "onProgress: $status")
                            }

                        })
            }
        }

        binding.sendMsg.setOnClickListener{
// 登录时一定要设置IM服务号
            AgoraMessage.newAgoraMessage().setCurrentChatUsername("kefuchannelimid_596534")

            if(ChatClient.getInstance().isLoggedInBefore()){
                //已经登录，可以直接进入会话界面
                var intent =  IntentBuilder(this@MainActivity)
                    .setServiceIMNumber("kefuchannelimid_596534") //获取地址：kefu.easemob.com，“管理员模式 > 渠道管理 > 手机APP”页面的关联的“IM服务号”
                    .build()
                startActivity(intent)
            }else{
                //未登录，需要登录后，再进入会话界面
            }
        }




    }
}